package com.ruoyi.web.controller.fire;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fire.domain.TBuilding;
import com.ruoyi.fire.service.ITBuildingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 建筑物Controller
 *
 * @author ruoyi
 * @date 2025-11-02
 */
@Api(value = "TBuildingController",tags = "建筑物")
@RestController
@RequestMapping("/system/building")
public class TBuildingController extends BaseController {
    @Autowired
    private ITBuildingService tBuildingService;

    /**
     * 查询建筑物列表
     */
    @ApiOperation("查询建筑物列表")
    @PreAuthorize("@ss.hasPermi('system:building:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBuilding tBuilding) {
        startPage();
        Long userId = SecurityUtils.getUserId();
        if (!SecurityUtils.isAdmin(userId)) {
            tBuilding.setCreateUserId(userId);
        }
        List<TBuilding> list = tBuildingService.selectTBuildingList(tBuilding);
        return getDataTable(list);
    }

    /**
     * 导出建筑物列表
     */
    @PreAuthorize("@ss.hasPermi('system:building:export')")
    @Log(title = "建筑物", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBuilding tBuilding) {
        List<TBuilding> list = tBuildingService.selectTBuildingList(tBuilding);
        ExcelUtil<TBuilding> util = new ExcelUtil<TBuilding>(TBuilding.class);
        util.exportExcel(response, list, "建筑物数据");
    }

    /**
     * 获取建筑物详细信息
     */
    @ApiOperation("获取建筑物详细信息")
    @PreAuthorize("@ss.hasPermi('system:building:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(tBuildingService.selectTBuildingById(id));
    }

    /**
     * 新增建筑物
     */
    @ApiOperation("新增建筑物")
    @PreAuthorize("@ss.hasPermi('system:building:add')")
    @Log(title = "建筑物", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBuilding tBuilding) {
        return toAjax(tBuildingService.insertTBuilding(tBuilding));
    }

    /**
     * 修改建筑物
     */
    @ApiOperation("修改建筑物")
    @PreAuthorize("@ss.hasPermi('system:building:edit')")
    @Log(title = "建筑物", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBuilding tBuilding) {
        return toAjax(tBuildingService.updateTBuilding(tBuilding));
    }

    /**
     * 删除建筑物
     */
    @ApiOperation("删除建筑物")
    @PreAuthorize("@ss.hasPermi('system:building:remove')")
    @Log(title = "建筑物", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tBuildingService.deleteTBuildingByIds(ids));
    }

    /**
     * 手动同步单位信息
     */
    @ApiOperation("手动同步建筑物信息")
    @Log(title = "手动同步建筑物信息", businessType = BusinessType.OTHER)
    @GetMapping("/push/{id}")
    public AjaxResult push(@PathVariable String id) {
        return toAjax(tBuildingService.pushTUnit(id));
    }


    /**
     * 远程删除建筑物
     */
    @ApiOperation("远程删除建筑物信息")
    @Log(title = "建筑物信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/remoteDeletion/{buildingId}")
    public AjaxResult remoteDeletion(@PathVariable String buildingId) {
        return toAjax(tBuildingService.remoteDeletion(buildingId));
    }

    /**
     * 先删除远程在删除本地信息
     */
    @ApiOperation("先删除远程在删除本地信息")
    @Log(title = "楼层信息", businessType = BusinessType.OTHER)
    @GetMapping("/remove/{buildingId}")
    public AjaxResult removeByBuildingId(@PathVariable String buildingId) {
        return toAjax(tBuildingService.removeByBuildingId(buildingId));
    }
}
